package cn.edu.gzccc.moviecms.controller;

import cn.edu.gzccc.moviecms.domain.Actor;
import cn.edu.gzccc.moviecms.repository.ActorRepository;
import org.omg.CORBA.PUBLIC_MEMBER;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author 杨友奇
 * @Time 2018/4/25
 * 角色管理
 */
@Controller
@RequestMapping(value = "actor")
public class ActorController {

    @Autowired
    private ActorRepository actorRepository;

    @RequestMapping(value = "/index")
    public String index(Model model) {
        List<Actor> list = actorRepository.findAll();
        model.addAttribute("info", list);
        return "actor/index";
    }

    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String add(Model model) {
        return "actor/add";
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public String add(@RequestParam String name,
                      @RequestParam Integer sex,
                      @RequestParam String born) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

        Date date = null;
        try {
            date = format.parse(born);
        } catch (Exception e) {
        }
        Actor actor = new Actor();
        actor.setName(name);
        actor.setSex(sex);
        actor.setBorn(date);
        actorRepository.save(actor);
        return "redirect:/actor/index";
    }

    //跳转到详细页面
    @RequestMapping(value = "/detail/{actor_id}", method = RequestMethod.GET)
    public String detail(Model model, @PathVariable Long actor_id) {
        Actor actor = actorRepository.findOne(actor_id);
        model.addAttribute("info", actor);
        return "actor/detail";
    }

    //跳转到编辑页面
    @RequestMapping(value = "edit/{id}", method = RequestMethod.GET)
    public String edit(Model model, @PathVariable Long id) {
        Actor actor = actorRepository.findOne(id);
        model.addAttribute("info", actor);
        return "actor/edit";
    }

    @RequestMapping(value = "edit/{id}", method = RequestMethod.POST)
    public String edit(@RequestParam String name,
                       @RequestParam Integer sex,
                       @RequestParam String born,
                       @RequestParam Long id) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

        Date date = null;
        try {
            date = format.parse(born);
        } catch (Exception e) {
        }
        Actor actor = actorRepository.findOne(id);
        actor.setName(name);
        actor.setSex(sex);
        actor.setBorn(date);
        actorRepository.save(actor);
        return "redirect:/actor/index";

    }
}
